//package com.Iconcat.system.handler;
//
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//import javax.naming.directory.Attributes;
//import java.io.IOException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.CopyOnWriteArrayList;
//
//@Component
//public class ChatHandler extends TextWebSocketHandler  {
//
//    private static final ConcurrentHashMap<Long, WebSocketSession> userSessions = new ConcurrentHashMap<>();
//    private static final ConcurrentHashMap<Long, WebSocketSession> adminSessions = new ConcurrentHashMap<>();
//
//
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        // 根据用户或管理员的信息，将 WebSocketSession 存储在对应的Map中
//        Long userId = (Long) session.getAttributes().get("userId");
//        Long adminId = (Long) session.getAttributes().get("adminId");
//
//        if (userId != null) {
//            userSessions.put(userId, session);
//        } else if (adminId != null) {
//            adminSessions.put(adminId, session);
//        }
//    }
//
//    @Override
//    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        // 处理接收到的文本消息
//        Long senderId = (Long) session.getAttributes().get("userId");
//        Long receiverId = (Long) session.getAttributes().get("adminId");
//
//        if (senderId != null && receiverId != null) {
//            // 将消息发送给管理员
//            WebSocketSession adminSession = adminSessions.get(receiverId);
//            if (adminSession != null && adminSession.isOpen()) {
//                adminSession.sendMessage(message);
//            }
//        } else if (receiverId != null && senderId == null) {
//            // 将消息发送给用户
//            WebSocketSession userSession = userSessions.get(receiverId);
//            if (userSession != null && userSession.isOpen()) {
//                userSession.sendMessage(message);
//            }
//        }
//    }
//
//    @Override
//    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        // 连接关闭时执行，从对应的Map中移除WebSocketSession
//        Long userId = (Long) session.getAttributes().get("userId");
//        Long adminId = (Long) session.getAttributes().get("adminId");
//        // 添加到 sessions 列表
////        sessions.add(session);
//        if (userId != null) {
//            userSessions.remove(userId);
//        } else if (adminId != null) {
//            adminSessions.remove(adminId);
//        }
//    }
//
//    public static void sendMessageToUser(Integer userId, String message) throws IOException {
//        WebSocketSession session = userSessions.get(userId);
//        if (session != null && session.isOpen()) {
//            session.sendMessage(new TextMessage(message));
//        }
//    }
//    public static void sendMessageToAdmin(Integer adminId, String message) throws IOException {
//        WebSocketSession session = adminSessions.get(adminId);
//        if (session != null && session.isOpen()) {
//            session.sendMessage(new TextMessage(message));
//        }
//    }
////    public void sendMessageToAll(String message) throws IOException {
////        for (WebSocketSession session : sessions) {
////            session.sendMessage(new TextMessage(message));
////        }
////    }
//}
